import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router'
import qs from 'querystring'
import Cookies from 'js-cookie'
// create an axios instance
const service = axios.create({
  baseURL: 'http://localhost:8080',
  // baseURL:'http://127.0.0.1:8888',
  timeout: 5000 // request timeout
})

// response interceptor
service.interceptors.response.use(
  response => {
    // res就是后端返回来的结果， { status,message,data,timestamp}
    const res = response.data

    // if the custom code is not 20000, it is judged as an error.
    if (res.code !== 200) {
      if (res.code === 401) {
        Message({ message: "请先登录", type: 'error', duration: 2 * 1000 })
        logout()
      }else {
        // 消息弹框
        Message({ message: res.msg, type: 'error', duration: 5 * 1000 })
      }
      // 返回承诺失败对象
      return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.msg,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)
export function get(url, params) {
  return service.get(url, {
    params, // get 请求时带的参数
    timeout: 10000,
    headers: {
      'X-Requested-With': 'XMLHttpRequest'
    }
  })
}
export function del(url, params) {
  return service.delete(url, {
    params, // get 请求时带的参数
    timeout: 10000,
    headers: {
      'X-Requested-With': 'XMLHttpRequest'
    }
  })
}

// 自定义post
export function post(url, data) {
  return service.post(url, qs.stringify(data), {
    timeout: 10000,
    headers: {
      'X-Requested-With': 'XMLHttpRequest',
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
  })
}
// 获取验证码
export function getCodeImg() {
  return service.request({
    url: '/captchaImage',
    headers: {
      isToken: false
    },
    method: 'get',
    timeout: 20000
  })
}

// 注册
export function register(data) {
  return service.request({
    url: '/system/tourist/add',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}

// 登录方法
export function login(data) {
  return service.request({
    url: '/login',
    headers: {
      isToken: false
    },
    method: 'post',
    data: data
  })
}

// 获取用户详细信息
export function getInfo() {
  return service.request({
    url: '/getInfo',
    method: 'get',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 修改用户个人信息
export function updateTourist(data) {
  return service.request({
    url: '/system/tourist/update',
    method: 'put',
    data: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 修改用户个人信息
export function updateUserProfile(data) {
  return service.request({
    url: '/system/user/profile',
    method: 'put',
    data: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

export function updateUserPwd(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword
  }
  return service.request({
    url: '/system/user/profile/updatePwd',
    method: 'put',
    params: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

export function updateTouristPwd(data) {
  return service.request({
    url: '/system/tourist/rest',
    method: 'put',
    data: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询用户个人信息
export function getUserProfile() {
  return service.request({
    url: '/system/user/profile',
    method: 'get',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询用户个人信息
export function getTourist() {
  return service.request({
    url: '/system/tourist/detail',
    method: 'get',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}
// 查询客房信息列表
export function listRoom(query) {
    return service.request({
        url: '/system/room/all',
        method: 'get',
        params: query,
        headers: {
            Authorization: "Bearer " + getToken(),
        },
    })
}

// 查询客房信息详细
export function getRoom(id) {
  return  service.request({
    url: '/system/room/' + id,
    method: 'get',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 新增客房订单
export function addRoomOrder(data) {
  return service.request({
    url: '/system/roomOrder/submit',
    method: 'post',
    data: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询餐饮列表
export function listRestaurant(query) {
  return service.request({
    url: '/system/restaurant/list',
    method: 'get',
    params: query,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询房间订单
export function getRoomOrder(query) {
  return service.request({
    url: '/system/roomOrder/my',
    method: 'get',
    params: query,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 查询餐饮订单
export function getRestaurantOrder(query) {
  return service.request({
    url: '/system/restaurantOrder/my',
    method: 'get',
    params: query,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 新增餐饮订单
export function addRestaurantOrder(data) {
  return service.request({
    url: '/system/restaurantOrder/add',
    method: 'post',
    data: data,
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}

// 删除餐饮订单
export function delRestaurantOrder(id) {
  return service.request({
    url: '/system/restaurantOrder/' + id,
    method: 'delete',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}
// 删除客房订单
export function delRoomOrder(id) {
  return service.request({
    url: '/system/roomOrder/' + id,
    method: 'delete',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}
// 取消客房订单
export function cancelRoomOrder(id) {
  return service.request({
    url: '/system/roomOrder/cancel/' + id,
    method: 'put',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}
// 取消客房订单
export function cancelRestaurantOrder(id) {
  return service.request({
    url: '/system/restaurantOrder/cancel/' + id,
    method: 'put',
    headers: {
      Authorization: "Bearer " + getToken(),
    },
  })
}
const TokenKey = 'Portal-Token'
export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}
// 退出
async function logout() {
  router.push(`/login`)
}

export default service
